import com.farm.FarmCoreApplication;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;


import org.jsoup.select.Elements;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.DigestUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SpringBootTest(classes = FarmCoreApplication.class)
@RunWith(SpringRunner.class)
public class test1 {
    private String result;

    @Test
    public void test11() {
        String pswd = DigestUtils.md5DigestAsHex(("admin" + "abcd").getBytes());
        System.out.println(pswd);
    }

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void test2() {
        redisTemplate.opsForValue().set("city", "guangzhou");
        Object city = redisTemplate.opsForValue().get("city");
        System.out.println(city);
    }

    @Test
    public void test3() {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet request = new HttpGet("http://znyj.nftec.agri.cn:8089/API/Market/GetList?token=0000000000000000&rid=25");
        try {
            HttpResponse response = httpClient.execute(request);
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                String result = EntityUtils.toString(entity);
                System.out.println(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test4(){
//        try {
//            Document doc = Jsoup.connect("https://www.shuichanq.com/plugin.php?id=xigua_hb&cat_id=395&province=&city=&dist=&orderby=&keyword=&filter=&ac=list_item&inajax=1&pagesize=20&page=1")
//                    .userAgent("Mozilla/5.0")
//                    .get();
//
//            System.out.println(doc);
//
//            Elements items = doc.select("a.card_row.view_jump");
//
//            for (Element item : items) {
//                String title = item.select("h3").text();
//                String description = item.select("div.car_info").text();
//                String imageUrl = item.select("img.card_abs_img").attr("src");
//
//                System.out.println("Title: " + title);
//                System.out.println("Description: " + description);
//                System.out.println("Image URL: " + imageUrl);
//                System.out.println("----------------------");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    @Test
    public void test6(){
        try {
            String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "<root><![CDATA[<a class=\"card_row view_jump\" data-id=\"6077\" data-stid=\"0\" target=\"_blank\" href=\"plugin.php?id=xigua_hb&ac=view&pubid=6077\">\n" +
                    "    <div class=\"card_left\" style=\"width:90px\">\n" +
                    "        <img class=\"card_abs_img\" src=\"https://img.shuichanq.com/pic/20240401/1711946729447869_352.png?imageView2/0/q/70/w/800\" onerror=\"this.error=null;this.src='source/plugin/xigua_hb/static/img/zhanwei.png'\">\n" +
                    "            </div>\n" +
                    "    <div class=\"card_right\" style=\"\"><h3 style=\"margin-bottom:4px\">\n" +
                    "                                    <span class=\"mod_lv is-red\">价格行情</span> 2024年4月1日对虾报价 </h3>\n" +
                    "        <div class=\"car_info\">虾蟹类-南美白对虾</div>                <div class=\"bt_box\">\n" +
                    "<ul class=\"tagsS\"><li class=\"card_car_tag blue0\"><i>批发价</i></li><li class=\"card_car_tag blue0\"><i>2024年4月1日对虾报价</i></li></ul></div>\n" +
                    "    </div>\n" +
                    "</a>\n" +
                    "<a class=\"card_row view_jump\" data-id=\"6076\" data-stid=\"0\" target=\"_blank\" href=\"plugin.php?id=xigua_hb&ac=view&pubid=6076\">\n" +
                    "    <div class=\"card_left\" style=\"width:90px\">\n" +
                    "        <img class=\"card_abs_img\" src=\"https://img.shuichanq.com/pic/20240401/171194668636230_843.png?imageView2/0/q/70/w/800\" onerror=\"this.error=null;this.src='source/plugin/xigua_hb/static/img/zhanwei.png'\">\n" +
                    "            </div>\n" +
                    "    <div class=\"card_right\" style=\"\"><h3 style=\"margin-bottom:4px\">\n" +
                    "                                    <span class=\"mod_lv is-red\">价格行情</span> 2024年4月1日小龙虾报价 </h3>\n" +
                    "        <div class=\"car_info\">虾蟹类-小龙虾</div>                <div class=\"bt_box\">\n" +
                    "<ul class=\"tagsS\"><li class=\"card_car_tag blue0\"><i>批发价</i></li><li class=\"card_car_tag blue0\"><i>2024年4月1日小龙虾报价</i></li></ul></div>\n" +
                    "    </div>\n" +
                    "</a>\n" +
                    "<a class=\"card_row view_jump\" data-id=\"6075\" data-stid=\"0\" target=\"_blank\" href=\"plugin.php?id=xigua_hb&ac=view&pubid=6075\">\n" +
                    "    <div class=\"card_left\" style=\"width:90px\">\n" +
                    "        <img class=\"card_abs_img\" src=\"https://img.shuichanq.com/pic/20240401/1711946622626107_213.png?imageView2/0/q/70/w/800\" onerror=\"this.error=null;this.src='source/plugin/xigua_hb/static/img/zhanwei.png'\">\n" +
                    "            </div>\n" +
                    "    <div class=\"card_right\" style=\"\"><h3 style=\"margin-bottom:4px\">\n" +
                    "                                    <span class=\"mod_lv is-red\">价格行情</span> 2024年4月1日苏州南环桥批发市场部分水产品价 </h3>\n" +
                    "        <div class=\"car_info\">海水鱼类-大黄鱼</div>                <div class=\"bt_box\">\n" +
                    "<ul class=\"tagsS\"><li class=\"card_car_tag blue0\"><i>批发价</i></li><li class=\"card_car_tag blue0\"><i>2024年4月1日苏州南环桥批发市场部分水产品价</i></li></ul></div>\n" +
                    "    </div>\n" +
                    "</a>]]></root>";
            String regex = "<div class=\"car_info\">(.*?)</div>(.*?)<li class=\"card_car_tag blue0\"><i>(.*?)</i>";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(xmlString);

            while (matcher.find()) {
                String divContent = matcher.group(1);
                System.out.println(divContent);
                divContent = matcher.group(3);
                System.out.println(divContent);
                System.out.println("--------------------------------------------------");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
